package cn.zjh.org.service.impl;

import cn.zjh.basic.exception.BusinessException;
import cn.zjh.basic.service.impl.BaseServiceImpl;
import cn.zjh.basic.util.BaiduAuditUtils;
import cn.zjh.org.domain.Employee;
import cn.zjh.org.domain.Shop;
import cn.zjh.org.domain.ShopAuditLog;
import cn.zjh.org.dto.ShopDto;
import cn.zjh.org.mapper.EmployeeMapper;
import cn.zjh.org.mapper.ShopAuditLogMapper;
import cn.zjh.org.mapper.ShopMapper;
import cn.zjh.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop){

        //:1.校检
        if(StringUtils.isEmpty(shop.getName())||StringUtils.isEmpty(shop.getTel())
        ||StringUtils.isEmpty(shop.getAddress())||StringUtils.isEmpty(shop.getEmployee().getUsername())
        ||StringUtils.isEmpty(shop.getEmployee().getEmail())||StringUtils.isEmpty(shop.getEmployee().getPhone())
        ||StringUtils.isEmpty(shop.getEmployee().getPassword())){
        throw new BusinessException("数据不能为空");
        }

        //密码是否一致

        //3.校验店铺的名称是否合规
        Boolean boo = BaiduAuditUtils.TextCensor(shop.getName());
        if(!boo){
            throw new BusinessException("店铺名称不合规!!!");
        }

        //是否已经入驻过了
        Shop shopTmp = shopMapper.loadByMessage(shop);
        if (shopTmp != null){
            throw new BusinessException ("已经入驻，请直接登录；如果忘记密码请联系管理员修改密码");
        }

        //二：添加到数据库
        //1.添加t_employee
        Employee employee = shop.getEmployee();
        employeeMapper.save(employee);

        //2.添加t_shop
        shop.setAdmin_id(employee.getId());
        shopMapper.save(shop);

        //3.修改t_employee的shop_id
        employee.setShop_id(shop.getId());
        employeeMapper.upDate(employee);

    }


//    private Logininfo employee2Logininfo(Employee employee) {
//        Logininfo logininfo = new Logininfo();
//        BeanUtils.copyProperties(employee,logininfo);
//        //设置为管理员
//        logininfo.setType(0);
//        return logininfo;
//    }


    @Transactional
    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.修改状态：state = 2
        Shop shop = shopMapper.findOne(log.getShop_id());
        shop.setState(2);
        shopMapper.upDate(shop);

        //2.添加审核日志到数据库
        log.setState(2);
        log.setAudit_id(352L);
        shopAuditLogMapper.save(log);

        //3.发送邮件：审核通过邮件 - 叫他激活
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方
        helper.setFrom("1028465808@qq.com");
        //设置主题
        helper.setSubject("店铺激活邮件");
        //设置内容
        helper.setText("<h1>你的店铺已经注册!!!</h1>" +
         "<a href='http://172.16.10.186:8080/shop/active/"+shop.getId()+"'>"+ "<img src='http://123.207.27.208/"+shop.getLogo()+"' width='150px'>" +"  点击该链接激活</a>",true);
        //设置收件人http://127.0.0.1:80
        Employee employee = employeeMapper.findOne(shop.getAdmin_id());
        helper.setTo("2458200699@qq.com");
        //发送
        javaMailSender.send(mimeMessage);

    }


    @Transactional
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        Shop shop = shopMapper.findOne(log.getShop_id());

        //2.添加审核日志到数据库
        log.setState(4);
        log.setAudit_id(325L);
        shopAuditLogMapper.save(log);


        //3.发送邮件：驳回邮件 - 告诉他审核失败，重新入驻
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方
        helper.setFrom("1028465808@qq.com");
        //设置主题
        helper.setSubject("入驻失败邮件");
        //设置内容
        helper.setText("<h1>入驻失败!!!</h1>" +
                "<a href='http://localhost:8081/#/register'>点击重新入驻</a>",true);
        //设置收件人
        Employee employee = employeeMapper.findOne(shop.getAdmin_id());
        //以后都是发送给店铺入驻时填写的邮箱
        helper.setTo("2458200699@qq.com");
        //发送
        javaMailSender.send(mimeMessage);

        //4.删除入驻信息
        //删除管理员信息
        employeeMapper.del(shop.getAdmin_id());
        //删除店铺信息
        shopMapper.del(shop.getId());
    }

    @Override
    public List<ShopDto> getShopState() {
        return shopMapper.getShopState();
    }

    @Override
    public void active(Long id) {
        Shop shop = shopMapper.findOne(id);
        shop.setState(3);
        shopMapper.upDate(shop);
    }
}
